package guoanmaker.personal.business.repository;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;


import guoanmaker.personal.business.model.PersonalUser;


/**
*
* 
* @author vladimir.stankovic
*
* Aug 16, 2016
*/
public interface PersonalUserRepository extends JpaRepository<PersonalUser, String>, JpaSpecificationExecutor<PersonalUser>{
	@Query("select u from PersonalUser u left join fetch u.roles r where u.username=:username")
	public Optional<PersonalUser> findByUsername(@Param("username") String username);
	@Query("select u from PersonalUser u  where u.username=:username")
	public List<PersonalUser> findUsername(@Param("username") String username);
/*	@Query("select u from PersonalUser u where u.wechatopenid=:wechatopenid and u.thirdtype=:thirdtype")
	public List<PersonalUser>  loginSelectThird(@Param("wechatopenid") String wechatopenid,@Param("thirdtype") String thirdtype);*/
	@Query("select u from PersonalUser u where u.username=:username")
	public List<PersonalUser> findUserByUsername(@Param("username")String username);
	
	@Query("select p from PersonalUser p where p.wechatopenid=:wechatopenid")
	public List<PersonalUser> findUserByWechatopenid(@Param("wechatopenid")String wechatopenid);
	@Query("select p from PersonalUser p where p.qqopenid=:qqopenid")
	public List<PersonalUser> findUserByqqopenid(@Param("qqopenid")String qqopenid);
	@Query("select p from PersonalUser p where p.weibouuid=:weibouuid")
	public List<PersonalUser> findUserByWeibouuid(@Param("weibouuid")String weibouuid);
	@Query("select p from PersonalUser p where p.wechatopenid=:wechatopenid and p.username=:username")
	public List<PersonalUser> findUserByWopenidorp(@Param("wechatopenid")String wechatopenid,@Param("username") String username);
	@Query("select p.username from PersonalUser p where p.id=:id")
	public String findUserNameByUserid(@Param("id")String id);

	@Modifying
	@Query("update PersonalUser p set p.wechatnickname=:wechatnickname,p.createTime=:createTime,p.wechatportraitN=:wechatportraitN  where p.username=:username")
	public int updatePersonalUserBynp(@Param("wechatnickname")String wechatnickname,@Param("createTime") Date createTime,@Param("wechatportraitN") String wechatportraitN
			,@Param("username")String username);
	

}
